const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList:[],
    id:'',
    productList:[],

    value: "",
    activeKey: 0,
    activeIndex: 0,
    items: [{ text: "百果园" }, { text: "时令鲜果" }, { text: "家禽蛋" }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCategoryList()
  },

  onChange(event) { 
    let index = event.detail;
    let id = this.data.categoryList[index].id;
    this.getProductList(id);
  },

  getCategoryList() {
    let url = '/category/all';
    app.get(url).then(res=> {
      this.setData({
        categoryList: res.data
      })
      this.getProductList(res.data[0].id);
    })
  },


  getProductList: function (id) { 
    let url = '/product/list?category_id=' + id;
    app.get(url).then(res=> {
      this.setData({
        productList: res.data
      })
    }).catch(err=> {
      console.log(err);
    })
  }
})